﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKDragCurveJig;
using HYView;

namespace YJKArch.SlopeDim
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	internal class CmdSlopeDim : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Autodesk.Revit.DB.Document document = commandData.Application.ActiveUIDocument.Document;
			if ((int)document.ActiveView.ViewType != 1 && (int)document.ActiveView.ViewType != 2)
			{
				Result result = SwitchView.SetUsableView((CmdViewType)3, commandData);
				if (result != null)
				{
					return result;
				}
			}
			using (SlopeDim slopeDim = new SlopeDim(commandData))
			{
				slopeDim.Show(WindowHelper.HWndRevit);
				WindowHelper.SendMessageToWndRevit();
				for (;;)
				{
					try
					{
						XYZ ptBase = null;
						List<JigEdgeInfo> list = new List<JigEdgeInfo>();
						List<JigTextInfo> list2 = new List<JigTextInfo>();
						List<CurveLoop> clList = new List<CurveLoop>();
						SlopeDimHelper.CreateDimData(slopeDim, document.ActiveView, ref ptBase, ref list, ref list2, ref clList);
						XYZ ptInsert = null;
						CmdSlopeDim.v = new ValueChanged(slopeDim.cbxValue.Text, slopeDim.txtAngle.Text);
						if (!DragCurveJigInterface.GetInsertPosition(commandData, list, false, list2, false, ptBase, ref ptInsert))
						{
							if (!slopeDim.IsContinueCmd)
							{
								slopeDim.Close();
								break;
							}
							slopeDim.IsContinueCmd = false;
						}
						else
						{
							Transaction transaction = new Transaction(document);
							transaction.Start("SlopeDim");
							ElementSet eleSet = new ElementSet();
							SlopeDimHelper.CreateDim(document, list, list2, clList, ptInsert, ref eleSet);
							RevitVersionFuncs.NewGroup(document, eleSet);
							transaction.Commit();
						}
					}
					catch (Exception)
					{
						slopeDim.Close();
						break;
					}
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public static ValueChanged v = new ValueChanged();
	}
}
